#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <err.h>
#include <libmscommon/mscommon.h>
#include <libmscommon/msepoll.h>
#include <libmslog/mslog.h>
#include <libmsprotocol/msprotocol.h>

#define FLAG "samlpe_srtclient"
static char * pinurl=ms_null;
static char * pouturl=ms_null;
int main(int argc, char **argv)
{
	mslog_api_setopt(53,ms_null,0);
	if(3!=argc){
		ms_error("usage:%s   inurl   outurl",argv[0]);
		mslog_api_unsetopt();
		return -1;
	}
	pinurl=argv[1];
	pouturl=argv[2];
	msprotocol_api_init();
	register_protocol();
	//srt_setloglevel(4);
	URLProtocol *msin_ptl=msprotocol_api_match(PTLNAME_SRT);
	URLProtocol *msout_ptl=msprotocol_api_match(PTLNAME_UDP);
	URLContext in_ctt;
	URLContext out_ctt;
	ms_memset(&in_ctt,0,sizeof(URLContext));
	ms_memset(&out_ctt,0,sizeof(URLContext));
	ms_strcpy(in_ctt.url, pinurl);
	ms_strcpy(out_ctt.url, pouturl);
	out_ctt.urlctt_opt.udp_opt.interfaceout="lo";
	out_ctt.urlctt_opt.udp_opt.localhostipout="127.0.0.1";
	out_ctt.flags|=FLAG_WRITE;
	if(msin_ptl->url_open(&in_ctt)<0){
		ms_errexit(-1, "Open in url failed");
	}
	
	out_ctt.urlctt_opt.srt_opt.listen=1;
	if(msout_ptl->url_open(&out_ctt)<0){
		ms_errexit(-1, "Open out url failed");
	}
	char buf[2048]={0};
	int len=0;
	while(1){
		len=msin_ptl->url_read(&in_ctt,buf,1316);
		if(len>0){
			msout_ptl->url_write(&out_ctt,buf,len);
		}
		ms_usleep(500);
	}	
	msin_ptl->url_close(&in_ctt);
	msout_ptl->url_close(&out_ctt);
	msprotocol_api_deinit();
	mslog_api_unsetopt();
}
#undef MAIN_C

